package com.efesco.finance;

import com.bstek.dorado.data.*;
import com.bstek.dorado.common.*;
import com.bstek.dorado.data.db.SqlDataset;
import com.bstek.dorado.view.data.ViewField;
import com.bstek.dorado.view.data.ViewBaseField;


/**
 * test_dataset1Listener
 */
public class test_dataset1Listener extends AbstractDatasetListener {

  public boolean beforeLoadData(Dataset dataset)
     throws java.lang.Exception {

   SqlDataset productDataset = new SqlDataset();
   productDataset.setSql("select distinct(pt.product_id) product_id,pt.product_name " +
                         "from s_employee emp ,s_employee_on_off_post eoop ,s_employee_contract_fee ecf,s_employee_contract_fee_item ecfi,s_product pt "+
                         "where emp.employee_id=eoop.employee_id and eoop.employee_on_off_id=ecf.employee_on_off_id "+
                         "and ecf.employee_contract_fee_id=ecfi.employee_contract_fee_id and ecfi.product_id=pt.product_id "+
                         "and emp.employee_id=6622211   ");
   productDataset.load( true);
   productDataset.moveFirst();

   while (!productDataset.isLast()){
     ViewBaseField field = (ViewBaseField)dataset.addField(productDataset.getString("product_id"));
     field.setLabel(productDataset.getString("product_name"));
     field.setDataType("double");
     field.setDefaultValue("0");
     productDataset.moveNext();
   }
   return true;
  }

  public void afterLoadData(Dataset dataset)
     throws Exception {

   String ecfId="";
   SqlDataset detailDataset = new SqlDataset();
   detailDataset.setSql("select emp.employee_id,emp.employee_name,ecf.employee_contract_fee_id,ecf.start_date,ecf.end_date,pt.product_id,ecfi.money " +
                         "from s_employee emp ,s_employee_on_off_post eoop ,s_employee_contract_fee ecf,s_employee_contract_fee_item ecfi,s_product pt "+
                         "where emp.employee_id=eoop.employee_id and eoop.employee_on_off_id=ecf.employee_on_off_id "+
                         "and ecf.employee_contract_fee_id=ecfi.employee_contract_fee_id and ecfi.product_id=pt.product_id "+
                         "and emp.employee_id=6622211 order by ecf.employee_contract_fee_id  ");
    detailDataset.load( true);
    detailDataset.moveFirst();

    while (!detailDataset.isLast()){
      if (ecfId.equals(detailDataset.getString("employee_contract_fee_id"))) {
        dataset.setString(detailDataset.getString("product_id"),detailDataset.getString("money"));
      }
      else  {
        dataset.insertRecord();
        ecfId=detailDataset.getString("employee_contract_fee_id");
        dataset.setString("employee_id",detailDataset.getString("employee_id"));
        dataset.setString("employee_name",detailDataset.getString("employee_name"));
        dataset.setString("start_date",detailDataset.getString("start_date"));
        dataset.setString("end_date",detailDataset.getString("end_date"));
        dataset.setString(detailDataset.getString("product_id"),detailDataset.getString("money"));
      }
      detailDataset.moveNext();
    }



  }


}
